package java复习202407;

import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class c11_lock锁验证 extends Thread {
    static Lock lock = new ReentrantLock();
    static 共享对象 共享对象 = new 共享对象();

    @Override
    public void run() {
        try {
//            共享对象.lock锁方法();
//            共享对象.tryLock锁方法();
//            共享对象.普通锁中断验证();
            共享对象.响应中断锁验证();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void lock锁尝试获取锁时是否可被打断验证() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(currentThread()+"开始尝试获取锁");
                lock.lock();
                System.out.println("获取到锁开始执行任务");
                lock.unlock();
                System.out.println("任务执行完成释放锁");
            }
        });
        lock.lock();
        t1.start();
        Thread.sleep(2000);
        t1.interrupt();
        System.out.println("打断完成");
        Thread.sleep(2000);
    }

    @Test
    public void lockInterruptibly锁尝试加锁时状态是否可被打断验证() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(currentThread()+"开始尝试获取锁");
                    lock.lockInterruptibly();
                    System.out.println("获取到锁开始执行任务");
                    lock.unlock();
                    System.out.println("任务执行完成释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        lock.lock();
        Thread.sleep(2000);
        t1.start();
        t1.interrupt();
        System.out.println("打断完成");
        Thread.sleep(2000);
    }

    @Test
    public void lockInterruptibly锁加锁成功后是否可被打断验证() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(currentThread()+"开始尝试获取锁");
                    lock.lockInterruptibly();
                    System.out.println("获取到锁开始执行任务");
                    自旋方法();
                    lock.unlock();
                    System.out.println("任务执行完成释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        Thread.sleep(1000);
        t1.interrupt();
        System.out.println("打断完成");
        Thread.sleep(2000);
    }

    static class 共享对象{
        public void lock锁方法() throws InterruptedException {
            lock.lock();
            System.out.println(Thread.currentThread()+"获取到锁");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread()+"代码执行结束，释放锁");
            lock.unlock();
        }

        public void tryLock锁方法() throws InterruptedException {
            while (! lock.tryLock() ){
            }

            System.out.println(Thread.currentThread()+"获取到锁");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread()+"代码执行结束，释放锁");
            lock.unlock();
        }

        public void 普通锁中断验证() throws InterruptedException {
            lock.lock();
            System.out.println(Thread.currentThread()+"获取到锁");
            Thread.sleep(3000);
            System.out.println(Thread.currentThread()+"代码执行结束，释放锁");
            lock.unlock();
        }

        public void 响应中断锁验证() throws InterruptedException {
            lock.lockInterruptibly();
            System.out.println(Thread.currentThread()+"获取到锁");
            Thread.sleep(3000);
            System.out.println(Thread.currentThread()+"代码执行结束，释放锁");
            lock.unlock();
        }

    }


    @Test
    public void lock锁验证() throws InterruptedException {
        c11_lock锁验证 thread1 = new c11_lock锁验证();
        c11_lock锁验证 thread2 = new c11_lock锁验证();
        thread1.start();
        thread2.start();

        Thread.sleep(5000);
    }

    @Test
    public void running锁中断验证() throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                System.out.println("获取到锁");
                Date endDate = new Date(System.currentTimeMillis() + 5000);
                while (new Date().compareTo(endDate) < 0){
                }
                System.out.println("代码执行完成，释放锁");
                lock.unlock();
            }
        });
        t.start();
        Thread.sleep(2000);
        t.interrupt();
        System.out.println("打断操作已执行");
        Thread.sleep(5000);
    }

    public void 自旋方法(){
        System.out.println("自旋开始");
        Date endDate = new Date(System.currentTimeMillis() + 5000);
        while (new Date().compareTo(endDate) < 0){
        }
        System.out.println("自旋结束");
    }
    @Test
    public void running响应锁中断验证() throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lockInterruptibly();
                    System.out.println("获取到锁");
                    Date endDate = new Date(System.currentTimeMillis() + 5000);
                    while (new Date().compareTo(endDate) < 0){
                    }
                    System.out.println("代码执行完成，释放锁");
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("打断成功");
                }
            }
        });
        t.start();
        Thread.sleep(1000);
        t.interrupt();
        System.out.println("打断操作已执行");
        Thread.sleep(5000);
    }

    @Test
    public void 响应中断锁验证锁获取不到锁中断验证() throws InterruptedException {
        c11_lock锁验证 thread1 = new c11_lock锁验证();
        lock.lock();
        thread1.start();
        Thread.sleep(1000);
        thread1.interrupt();
        System.out.println("打断完成");
        Thread.sleep(5000);
    }

    @Test
    public void 响应中断锁验证锁获取到锁中断验证() throws InterruptedException {
        c11_lock锁验证 thread1 = new c11_lock锁验证();
        thread1.start();
        Thread.sleep(1000);
        thread1.interrupt();
        System.out.println("打断完成");
        Thread.sleep(5000);
    }


}
